package com.api.service.impl;

import com.api.model.entity.BackstageUser;
import com.api.model.entity.Menu;
import com.api.model.request.BackstageUserLoginRequest;
import com.api.model.request.IsUserMenuRequest;
import com.api.model.request.MenuListRequest;
import com.api.model.response.NullResponse;
import com.api.common.reqres.Request;
import com.api.common.reqres.RequestHead;
import com.api.common.reqres.Response;
import com.api.component.RequestComponent;
import com.api.component.ResponseComponent;
import com.api.mapper.BackstageUserMapper;
import com.api.mapper.MenuMapper;
import com.api.mapper.RoleMapper;
import com.api.mapper.RoleMenuMapper;
import com.api.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfp
 * @since 2019-10-22
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
	@Autowired
	private RequestComponent requestComponent;
	@Autowired
	private ResponseComponent responseComponent;
	@Autowired
	private MenuMapper menuMapper;

	@Override
	public Response<NullResponse> menuList(Request<MenuListRequest> request) {
		// TODO Auto-generated method stub
		LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<Menu>();
		queryWrapper.eq(Menu::getStatus, true);
		List<Menu> menuList = menuMapper.selectList(null);
		return responseComponent.toSuccess(menuList);
	}

	@Override
	public Response<NullResponse> isUserMenu(Request<IsUserMenuRequest> request) {
		// TODO Auto-generated method stub
		RequestHead head = request.getHead();
		IsUserMenuRequest isUserMenuRequest = request.getBody();
		requestComponent.checkBody(isUserMenuRequest, IsUserMenuRequest.class);
		Menu menu = new Menu();
		menu.setUpdatetime(LocalDateTime.now());
		menu.setUpdateuser(head.getUserid());
		BeanUtils.copyProperties(isUserMenuRequest, menu);
		menuMapper.updateById(menu);
		return responseComponent.toSuccess(null);
	}

}
